﻿using System;
using System.Net;
using System.Net.Sockets;

namespace Ndns.Packet.Abstractions.ResourceRecords
{
    public sealed class DnsResourceRecordA : DnsResourceRecord
    {
        public override DnsHostName HostName { get; set; }
        public override DnsRecordType Type { get => DnsRecordType.A; protected set => throw new NotSupportedException(); }
        public override DnsClass Class { get; protected set; }

        /// <summary>
        /// 表示应答中的A记录。
        /// </summary>
        public IPAddress Address { get; set; }

        public DnsResourceRecordA(DnsHostName hostName, DnsClass c, uint ttl)
            : base(hostName, c, ttl)
        {
        }

        /// <summary>
        /// 根据相关信息构造DnsResourceRecordCNAME实例。
        /// </summary>
        /// <param name="hostName">DNS应答的主机名信息。</param>
        /// <param name="c">DNS应答的类别。</param>
        /// <param name="ttl">DNS应答的超时时间。</param>
        /// <param name="address">DNS应答的数据。</param>
        public DnsResourceRecordA(DnsHostName hostName, DnsClass c, uint ttl, IPAddress address)
            : base(hostName, c, ttl)
        {
            if (address == null)
                throw new ArgumentNullException(nameof(address));
            if (address.AddressFamily != AddressFamily.InterNetwork)
                throw new ArgumentOutOfRangeException(nameof(address));
            Address = address;
        }

        public override DnsResourceRecord Clone()
        {
            return new DnsResourceRecordA(HostName, Class, TTL, Address);
        }

    }
}
